package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.FormalStudent;
import cn.wolfcode.edu.domain.TuitionIncome;
import cn.wolfcode.edu.mapper.FormalStudentMapper;
import cn.wolfcode.edu.mapper.TuitionIncomeMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.query.TuitionIncomeQuery;
import cn.wolfcode.edu.service.ITuitionIncomeService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * Created by 徐沅澧 on 2018/7/11.
 */
@Service
public class TuitionIncomeServiceImpl implements ITuitionIncomeService {
    @Autowired
    private TuitionIncomeMapper incomeMapper;
    @Autowired
    private FormalStudentMapper studentMapper;

    @Override
    public PageResult query(TuitionIncomeQuery qo) {
        int total = incomeMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<TuitionIncome> rows = incomeMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void save(TuitionIncome income) {
        FormalStudent stu = studentMapper.selectByPrimaryKey(income.getStudent().getId());
        //不用表单传过来的已付
        income.setAlreadyPaid(stu.getAlreadyPaid());
        //不用表单传过来的待付
        income.setRemainPaid(stu.getRemainPaid());
        //随机生成单据号
        income.setReceipt(UUID.randomUUID().toString());
        incomeMapper.insert(income);
    }

    @Override
    public void update(TuitionIncome income) {
        FormalStudent stu = studentMapper.selectByPrimaryKey(income.getStudent().getId());
        //不用表单传过来的已付
        income.setAlreadyPaid(stu.getAlreadyPaid());
        //不用表单传过来的待付
        income.setRemainPaid(stu.getRemainPaid());
        incomeMapper.updateByPrimaryKey(income);
    }

    @Override
    public void deleteById(Long id) {
        incomeMapper.deleteById(id);
    }

    @Override
    public void auditById(Long id) {
        //根据Id查出数据
        TuitionIncome income = incomeMapper.selectByPrimaryKey(id);
        //根据查出的数据查出学生信息
        FormalStudent student = studentMapper.selectByPrimaryKey(income.getStudent().getId());
        //设置审核人和审核状态
        income.setStatus(TuitionIncome.ALREADY_STATUS);
        income.setAuditer((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置已付金额
        income.setAlreadyPaid(student.getAlreadyPaid().add(income.getAmountMoney()));
        student.setAlreadyPaid(student.getAlreadyPaid().add(income.getAmountMoney()));
        //设置剩余应还金额
        income.setRemainPaid(student.getRemainPaid().subtract(income.getAmountMoney()));
        student.setRemainPaid(student.getRemainPaid().subtract(income.getAmountMoney()));
        //如果student的剩余应还金额等于0//设置付款状态为已缴清,否则未缴清
        if (student.getRemainPaid().equals(BigDecimal.ZERO)) {
            student.setPaidStatus(FormalStudent.ALREADY_PAID);
        } else {
            student.setPaidStatus(FormalStudent.NOT_YET_PAID);
        }
        //更新
        incomeMapper.updateByPrimaryKey(income);
        studentMapper.updateByPrimaryKey(student);
    }
}